package com.example.shopping_li.Fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shopping_li.R;
import com.example.shopping_li.cart.CartActivity;
import com.example.shopping_li.me.PersonCenterActivity;
import com.example.shopping_li.me.UserBean;
import com.example.shopping_li.me.loginActivity;
import com.example.shopping_li.utils.SaveUserUtils;

import java.io.File;
//我的界面碎片

    public class MeFragment extends Fragment implements View.OnClickListener {
        Button cartBtn,historyBtn,centerBtn,aboutBtn,settingBtn;
        ImageView headIv;
        TextView nicknameTv;
        private UserBean.DataBean dataBean;
        String filepath;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_me, container, false);
            initView(view);
            return view;
        }
        @Override
        public void onResume() {
            super.onResume();
            onResumePage();
        }
        //获取焦点时会调用的方法
        public void onResumePage() {
            dataBean = SaveUserUtils.getUserinfo();
            headIv.setImageResource(R.mipmap.icon);
            if (dataBean!=null) {
                String nickname = dataBean.getNickname();
                nicknameTv.setText(nickname);
                //获取图片文件地址
                filepath = getContext().getCacheDir().getAbsolutePath()+ File.separator+dataBean.getPhone()+".png";
                File headfile = new File(filepath);
                if (headfile.exists()) {
//                Picasso.with(getActivity()).load(headfile).into(headIv);
                    Bitmap bitmap = BitmapFactory.decodeFile(filepath);
                    headIv.setImageBitmap(bitmap);
                }
            }else{
                nicknameTv.setText("请先登录");
            }
        }
        /* 判断当前应用是否登录，已经登录，跳转到个人中心页面。未登录，跳转到登录页面*/
        private void startCenterPage(){
            if (dataBean==null) {
                Toast.makeText(getContext(),"还未登录，请先登录账号！",Toast.LENGTH_SHORT).show();
                Intent it = new Intent(getActivity(), loginActivity.class);
                startActivity(it);
            }else{
                Intent it = new Intent(getActivity(), PersonCenterActivity.class);
                startActivity(it);
            }
        }

        private void initView(View v) {
            cartBtn = v.findViewById(R.id.frag_me_btn_cart);
            historyBtn = v.findViewById(R.id.frag_me_btn_history);
            centerBtn = v.findViewById(R.id.frag_me_btn_center);
            aboutBtn = v.findViewById(R.id.frag_me_btn_about);
            settingBtn = v.findViewById(R.id.frag_me_btn_setting);
            headIv = v.findViewById(R.id.frag_me_iv);
            nicknameTv = v.findViewById(R.id.frag_me_tv_name);

            cartBtn.setOnClickListener(this);
            historyBtn.setOnClickListener(this);
            centerBtn.setOnClickListener(this);
            aboutBtn.setOnClickListener(this);
            settingBtn.setOnClickListener(this);
            headIv.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.frag_me_btn_cart:
                    if (dataBean==null) {
                        Toast.makeText(getContext(), "请先登录...", Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(getActivity(), CartActivity.class);
                        startActivity(intent);

                    }
                    break;
                case R.id.frag_me_btn_history:

                    break;
                case R.id.frag_me_btn_center:
                    startCenterPage();
                    break;
                case R.id.frag_me_btn_about:

                    break;
                case R.id.frag_me_btn_setting:

                    break;
                case R.id.frag_me_iv:
                    startCenterPage();
                    break;
            }

        }
    }